﻿using System.Text;

namespace Recast
{
    public class TypeModel
    {
        public string typeName;
        public string inheritType;
        public Dictionary<string, string> Fields = new Dictionary<string, string>();
        public Dictionary<string, MethodModel> Methods = new Dictionary<string, MethodModel>();
        public List<string> Defines = new List<string>();
        public bool IsClass, IsStruct, IsEnum;

        public TypeModel(string typeName)
        {
            this.typeName = typeName;
        }

        public TypeModel(string typeName, MethodModel method)
        {
            this.typeName = typeName;
            Methods.Add(method.Name, method);
        }

        public string ToCode()
        {
            var text = new StringBuilder();
            if (IsEnum)
            {
                text.AppendLine("namespace Recast");
                text.AppendLine("{");
                text.AppendLine("\tpublic enum " + typeName);
                text.AppendLine("\t{");
                foreach (var field in Fields)
                {
                    text.AppendLine("\t\t" + field.Key + ",");
                }
                text.AppendLine("\t}");
                text.AppendLine("}");
                return text.ToString();
            }
            if (IsClass | IsStruct)
            {
                foreach (var define in Defines)
                {
                    text.AppendLine(define);
                }
                text.AppendLine("namespace Recast");
                text.AppendLine("{");
                bool isGlobalClass = typeName == "ClassGlobal";
                if (!isGlobalClass)
                {
                    text.AppendLine("\tpublic unsafe partial class ClassGlobal");
                    text.AppendLine("{");
                }
                text.AppendLine($"\t\tpublic unsafe partial {(IsClass ? "class" : "struct")} " + typeName + (string.IsNullOrEmpty(inheritType) ? "" : " : " + inheritType));
                text.AppendLine("\t\t{");
                foreach (var field in Fields)
                {
                    if (isGlobalClass)
                        text.AppendLine("\t\t\tpublic static " + field.Value + " " + field.Key + ";");
                    else
                        text.AppendLine("\t\t\tpublic " + field.Value + " " + field.Key + ";");
                }
                foreach (var method in Methods)
                {
                    if (string.IsNullOrEmpty(method.Value.methodBody) && !method.Value.isDelegate)
                        continue;
                    if (isGlobalClass)
                        method.Value.isStatic = true;
                    text.AppendLine("\t\t\t" + method.Value.ToString());
                }
                text.AppendLine("\t\t}");
                if (typeName != "ClassGlobal")
                    text.AppendLine("\t}");
                text.AppendLine("}");
                return text.ToString();
            }
            return "";
        }

        public override string ToString()
        {
            var text = "";
            if (IsClass)
                text = "class ";
            if (IsStruct)
                text = "struct ";
            if (IsEnum)
                text = "enum ";
            text += typeName;
            return text;
        }
    }

    public unsafe class XX
    {
        public float x = 5;
    }
}
